package com.sihuatech.coreagent.send;


import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * The Class ResponseSender.<br>
 * 
 * @author: Brian<br>
 * Date: 2008-10-30<br>
 * Time: 13:52:14<br>
 * ProjectName: framework
 */
public class ResponseSender {
    
    /** The logger. */
    private static Log logger =	LogFactory.getLog(ResponseSender.class);

    /**
     * Send response.
     * 
     * @param responseBody 内容体
     * @param response HttpServletResponse
     * @param charset 字符集 EX:UTF-8,GBK,if charset == null charset defalut value GBK
     */
	public void sendResponse(String responseBody, HttpServletResponse response,String charset) {
		PrintWriter writer = null;
        try{
        	if (charset != null) {
        		response.setHeader("Content-type", "text/xml; charset=" + charset);
        	} else {
        		response.setHeader("Content-type", "text/xml; charset=GBK");
        	}
        	String strLength="";
        	if(responseBody==null||responseBody.trim().equals("")){
        		strLength="0";
        	}else{
        		strLength=String.valueOf(responseBody.getBytes().length);
        	}
			response.addHeader("Content-Length",strLength);
            writer = response.getWriter();
            writer.write(responseBody);
            writer.flush();
            writer.close();
            logger.info("返回消息");
            logger.info(responseBody);
        }catch(IOException ioe){
           logger.info("发送响应错误");
            logger.error(ioe);
        }
		
	}

}